import { createRouter, createWebHashHistory } from "vue-router";
const router = createRouter({
	history: createWebHashHistory(),
	routes: [{
			path: '/',
			redirect: '/login', //重定向
		},
		{
			path: '/login',
			component: () => import('@/views/admin/login/index.vue')
		},

		{
			path: '/home',
			component: () => import('@/views/admin/home/index.vue'),
			redirect: '/home/users', // 重定向
			children: [{
					path: 'users',
					component: () => import('@/views/admin/users/index.vue')
				},
				{
					path: 'rights',
					component: () => import('@/views/admin/rights/index.vue')
				},
				{
					path: 'goods',
					component: () => import('@/views/admin/goods/index.vue')
				},
				{
					path: 'orders',
					name: 'OrderList',
					component: () => import('@/views/admin/orders/index.vue')
				},
				{
					path: 'orders/:id',
					name: 'OrderDetail',
					component: () => import('@/views/admin/orders/OrderDetail.vue'),
					props: true
				},
				{
					path: 'settings',
					component: () => import('@/views/admin/setting/index.vue')
				},
				{
					path: 'users/:id',
					component: () => import('@/views/admin/users/usersDetail.vue'),
					props: true
				}
			]
		}
	]
})


router.beforeEach((to, from, next) => {
	// 如果访问的是登录页面，直接执行下一个钩子函数
	if (to.path === '/login')
		return next()
	// 获取Token值
	const token = localStorage.getItem('token')
	console.log(token)
	if (!token) {
		// Token值不存在，强制跳转到登录页面
		return next('/login')
	}
	// 存在Token值，直接执行下一个钩子函数
	next()
})


export default router